package com.wpml.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wpml.common.util.PageUtils;
import com.wpml.common.util.Query;
import com.wpml.product.dao.BrandDao;
import com.wpml.product.dao.CategoryBrandRelationDao;
import com.wpml.product.dao.CategoryDao;
import com.wpml.product.entity.BrandModel;
import com.wpml.product.entity.CategoryBrandRelationModel;
import com.wpml.product.entity.CategoryModel;
import com.wpml.product.service.BrandService;
import com.wpml.product.service.CategoryBrandRelationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 品牌分类关联(PmsCategoryBrandRelation)表服务实现类
 *
 * @author makejava
 * @since 2020-04-07 11:33:36
 */
@Service
@Slf4j
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationModel> implements CategoryBrandRelationService {

    @Resource
    BrandDao brandDao;

    @Resource
    CategoryDao categoryDao;

    @Resource
    CategoryBrandRelationDao relationDao;

    @Autowired
    BrandService brandService;

    public void updateCategory(Long catId, String name) {
        this.baseMapper.updateCategory(catId, name);
    }

    @Override
    public void updateBrand(Long brandId, String name) {
        CategoryBrandRelationModel relationEntity = new CategoryBrandRelationModel();
        relationEntity.setBrandId(brandId);
        relationEntity.setBrandName(name);
        this.update(relationEntity, new UpdateWrapper<CategoryBrandRelationModel>().eq("brand_id", brandId));
    }



    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationModel> page = this.page(
                new Query<CategoryBrandRelationModel>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDetail(CategoryBrandRelationModel categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        //1、查询详细名字
        BrandModel brandEntity = brandDao.selectById(brandId);
        CategoryModel categoryEntity = categoryDao.selectById(catelogId);

        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());

        this.save(categoryBrandRelation);
    }

    @Override
    public List<BrandModel> getBrandsByCatId(Long catId) {

        List<CategoryBrandRelationModel> catelogId = relationDao.selectList(new QueryWrapper<CategoryBrandRelationModel>().eq("catelog_id", catId));
        List<BrandModel> collect = catelogId.stream().map(item -> {
            Long brandId = item.getBrandId();
            BrandModel byId = brandService.getById(brandId);
            return byId;
        }).collect(Collectors.toList());
        return collect;
    }
}
